package com.asset.dispoal.file.plaintiffFile.service.commom;


import com.asset.dispoal.file.plaintiffFile.domain.db.entity.PlaintiffFile;
import com.asset.dispoal.file.plaintiffFile.domain.db.mapper.PlaintiffFileMapper;
import com.asset.dispoal.file.plaintiffFile.service.converter.PlaintiffFileConverter;
import com.asset.dispoal.plaintiffFile.dto.PlaintiffFileRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.exception.BusinessException;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.io.IOUtil;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @author qr
 * @date 2022/3/22$
 */
@Component
public class PlaintiffFileInsertCommand implements BaseCommand<PlaintiffFileRequest, Result> {

    @Autowired
    private Environment environment;

    @Autowired
    private BaseIdeable<String> idWorker;
    @Autowired
    PlaintiffFileMapper plaintiffFileMapper;

    @Override
    public Result execute(PlaintiffFileRequest request) {
        List<MultipartFile> businessLicenseFile = request.getBusinessLicenseFile();
        List<MultipartFile> idCardFile = request.getIdCardFile();
        List<MultipartFile> legalRepresentativeProveFile = request.getLegalRepresentativeProveFile();
        List<MultipartFile> sealFile = request.getSealFile();
        request.setFileType("business_license");
        uploadFile(businessLicenseFile, request);
        request.setFileType("id_card");
        uploadFile(idCardFile, request);
        request.setFileType("legal_person_identification");
        uploadFile(legalRepresentativeProveFile, request);
        request.setFileType("stamp_file");
        uploadFile(sealFile, request);
        return Result.ok();
    }

    private void uploadFile(List<MultipartFile> files, PlaintiffFileRequest request) {
        if (CollectionUtil.isEmpty(files)) {
            return;
        }
        String headUrl = environment.getProperty("file.evidence.url");
        String property = environment.getProperty("file.plaintiff.url");
        FileOutputStream fs = null;
        InputStream in = null;
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        int day = now.getDayOfMonth();
        //文件路径
        String resolveUrl = "/" +
                year +
                "/" +
                month +
                "/" +
                day;
        property = property + "/" + resolveUrl;
        File parentResolveFile = new File(headUrl + "/" + property);
        if (!parentResolveFile.exists()) {
            parentResolveFile.mkdirs();
        }
        for (MultipartFile multipartFile : files) {
            String generateId = idWorker.generateId();
            String originalFilename = multipartFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            try {
                multipartFile.transferTo(new File(headUrl + "/" + property + "/" + generateId + suffix));
                PlaintiffFile plaintiffFile = PlaintiffFileConverter.INSTANCE.reqToDo(request);
                plaintiffFile.setFileUrl("/" + property + "/" + generateId + suffix);
                plaintiffFile.setFileOrginalName(originalFilename);
                plaintiffFile.setId(generateId);
                plaintiffFileMapper.insert(plaintiffFile);
            } catch (Exception e) {
                throw new BusinessException(Result.SYS_FAILD, "上传处理失败", e);
            } finally {
                IOUtil.close(in, fs);
            }
        }
    }
}
